package com.uenpay.im.ui.old.activity.pwd;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.login.LoginAndRegisterHandle;
import com.uenpay.im.ui.old.activity.personal.UpdatePersonInfoCommonHandle;
import com.uenpay.im.weight.ZProgressHUD;
import com.uenpay.util.app.base.BaseActivity;

/**
 * @author SL
 * @copyRight http://www.qiyunxin.com
 * @date 2016-6-12 下午2:13:52
 * @version 1.0
 * @function 获取修改密码短信验证码
 *
 */
public class GetPayPwdVercodeActivity extends BaseActivity {

	private EditText verification_code_edit;
	private String new_pay_pwd;
	private ZProgressHUD mZProgressHUD;
	private Button get_verification_code_btn;
	/** 1:修改支付密码2：绑定银行卡 **/
	private int type;
	private String bankCard, bankName, accountname;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		isFullScreen=true;
		super.onCreate(savedInstanceState);
	}

	@Override
	public View bindView() {
		return null;
	}

	@Override
	public int bindLayout() {
		return R.layout.activity_pay_vercode_layout;
	}

	@Override
	public void initView(View view) {
		type = getIntent().getIntExtra("type", 1);
		if (type == 1) {
			new_pay_pwd = getIntent().getStringExtra("new_pay_pwd");
		} else {
			bankCard = getIntent().getStringExtra("bankCard");
			bankName = getIntent().getStringExtra("bankName");
			accountname = getIntent().getStringExtra("accountname");
		}
		initView();
		initListener();
		initTimer();
	}

	private void initTimer() {
		if (timer != null) {
			timer.start();
		}
		get_verification_code_btn.setEnabled(false);
		get_verification_code_btn
				.setBackgroundResource(R.drawable.botton_shape_bg_gray);
	}

	CountDownTimer timer = new CountDownTimer(60 * 1000, 1000) {
		@Override
		public void onTick(long millisUntilFinished) {
			get_verification_code_btn.setText(getResources().getString(
					R.string.send_code)
					+ "(" + (millisUntilFinished / 1000) + ")");
		}

		@Override
		public void onFinish() {
			get_verification_code_btn.setEnabled(true);
			// sendButtom.setTextColor(getResources().getColor(R.color.white));
			get_verification_code_btn
					.setBackgroundResource(R.drawable.button_bg_normal);
			get_verification_code_btn.setText(getResources().getString(
					R.string.send_code));
		}

	};

	protected void initView() {
		mZProgressHUD = new ZProgressHUD(this);
		((TextView) findViewById(R.id.title_textview))
				.setText(R.string.four_verification_code);
		verification_code_edit = (EditText) findViewById(R.id.verification_code_edit);
		get_verification_code_btn = (Button) findViewById(R.id.get_verification_code_btn);

	}

	protected void initListener() {
		findViewById(R.id.submit_btn).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				String verCode = verification_code_edit.getText().toString();
				if (type == 1) {
					if (!TextUtils.isEmpty(verCode)
							&& !TextUtils.isEmpty(new_pay_pwd)) {
						resetPayPwd(verCode);
					}
				}
			}
		});

		get_verification_code_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				getRegisterCaptcha(IMApplication.getUserMobilePhone());
			}
		});
	}

	/**
	 * 获取验证码
	 *
	 * @param phoneNum
	 */
	private void getRegisterCaptcha(String phoneNum) {
		LoginAndRegisterHandle loginAndRegisterHandle = new LoginAndRegisterHandle();
		loginAndRegisterHandle.getRegisterCaptcha(phoneNum, "pay",
				new LoginAndRegisterHandle.IRegisterCaptchaResultListener() {

					@Override
					public void onRegisterCaptchaResult(int status, String msg,
							String vercode) {
						if (status == HttpInvokeResultCode.RESULT_OK) {
							initTimer();
						} else {
							if (!TextUtils.isEmpty(msg))
								IMApplication.showToast(msg);
						}
					}
				});

	}

	/**
	 * 重置支付密码
	 *
	 * @param verCode
	 */
	private void resetPayPwd(String verCode) {
		mZProgressHUD.show();
		UpdatePersonInfoCommonHandle.resetPayPwd(new_pay_pwd, verCode,
				new UpdatePersonInfoCommonHandle.ISetPayPwdResultListener() {

					@Override
					public void onPayPwdResult(int status, String msg) {
						mZProgressHUD.dismiss();
						if (!TextUtils.isEmpty(msg)) {
							IMApplication.showToast(msg);
						}
						if (status == HttpInvokeResultCode.RESULT_OK) {
							finish();
						}
					}
				});
	}


	@Override
	public void onBackPressed() {
		finish();
		super.onBackPressed();
	}
}
